package com.ljm.travels.controller;

import com.ljm.travels.domain.Place;
import com.ljm.travels.domain.Result;
import com.ljm.travels.service.PlaceService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/place")
public class PlaceController {

    @Autowired
    private PlaceService placeService;
    //注入图片保存路径
    @Value("${upload.dir}")
    String realPath;

    @PostMapping("/update")
    private Result update(MultipartFile pic,Place place) throws IOException {
        Result result = new Result();
        //处理文件上传
        //对接受的文件做base64处理
        try{
            String picPath = Base64Utils.encodeToString(pic.getBytes());
            place.setPicPath(picPath);
            String extension = FilenameUtils.getExtension(pic.getOriginalFilename());
            String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + extension;
            pic.transferTo(new File(realPath,newFileName));
            placeService.update(place);
            result.setMsg("修改景点信心成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setState(false).setMsg("修改失败"+e.getMessage());
        }
        return result;
    }

    @GetMapping("/findOne")
    private Place findOne(Integer id){
        return placeService.finOne(id);
    }

    @GetMapping("/delete")
    private Result deletePlace(Integer id){
        Result result = new Result();
        try {
            placeService.delete(id);
            result.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setState(false).setMsg(e.getMessage());
        }
        return result;
    }
    
    
    @PostMapping("/save")
    private Result savePlace(MultipartFile pic, Place place) throws IOException {
        System.out.println(pic.getOriginalFilename());
        System.out.println(place.toString());
        Result result = new Result();
        try {
            //获取扩展名
            String extension = FilenameUtils.getExtension(pic.getOriginalFilename());
            String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + extension;
            //将上传前图片的二进制码转换成base64编码
            place.setPicPath(Base64Utils.encodeToString(pic.getBytes()));
            //将图片转化成新文件//文件上传
            pic.transferTo(new File(realPath,newFileName));
            //保存数据库
            placeService.save(place);
            result.setMsg("保存景点信息成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setState(false).setMsg(e.getMessage());

        }
        return result;
    }

    @GetMapping("/findAllPlace")
    private Map<String, Object> findAllPlace(Integer page, Integer rows, Integer provinceId) {
        HashMap result = new HashMap();
        page = page == null ? 1 : page;
        rows = rows == null ? 3 : rows;
        //景点集合
        List<Place> places = placeService.findByProvinceIdPage(page, rows, provinceId);
        //景点总数
        Integer counts = placeService.findByProvinceIdCounts(provinceId);
        //总页数
        Integer totalPage = counts % rows == 0 ? counts / rows : counts / rows + 1;
        result.put("places", places);
        result.put("page", page);
        result.put("totalPage", totalPage);
        result.put("counts", counts);
        return result;
    }

}
